package com.sergio.gestion.controller;

import java.awt.geom.CubicCurve2D;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.sergio.gestion.cto.SocioCto;
import com.sergio.gestion.entities.Socio;
import com.sergio.gestion.form.SocioForm;
import com.sergio.gestion.service.SocioService;
import com.sergio.gestion.util.Constantes;
import com.sergio.gestion.util.Converter;

@Controller
public class Controlador {
	@Autowired
	private SocioService socioService;
	@Autowired
	private Converter converter;
	protected final Log logger = LogFactory.getLog(Controlador.class);

	@RequestMapping(value = "/searchSocios.htm")
	public ModelAndView searchSocios(HttpServletRequest request,
			HttpServletResponse response, ModelMap model,@RequestParam(value="currentPage",required=false) Integer currentPage)
			throws ServletException, IOException {
		
		String now = (new Date()).toString();
		this.buscarSocios(model,currentPage);
		
		SocioForm socioForm = new SocioForm();
		model.addAttribute("socioForm", socioForm);
		model.addAttribute("fecha", now);
		return new ModelAndView("searchSocios", "model", model);
	}

	@RequestMapping(value = "/crearSocio.htm")
	public ModelAndView crearSocio(HttpServletRequest request,
			HttpServletResponse response, ModelMap model,@ModelAttribute("socioForm") SocioForm socioForm)
			throws ServletException, IOException {
		
		SocioCto socioCto=new SocioCto();
		socioCto=this.converter.toCto(socioForm);
		socioService.guardarSocio(socioCto);
		
		return new ModelAndView("searchSocios", "model", model);
	}

	private void buscarSocios(ModelMap model,Integer currentPage) {
		
		Integer totalSize;
		if (currentPage==null) {
			 currentPage = 1;
		}
		Integer firstResult=(currentPage-1)*Constantes.NUM_SOCIO_PAG;
		List<Socio> listaSocios = this.socioService
				.obtenerSociosAll(firstResult,Constantes.NUM_SOCIO_PAG);
		totalSize=this.socioService.countSociosAll();
		Long totalPages=Math.round((totalSize / (double) Constantes.NUM_SOCIO_PAG) + 0.49d);
		model.addAttribute("currentPage",currentPage);
		model.addAttribute("totalSize",totalSize);
		model.addAttribute("totalPages", totalPages);
		model.addAttribute("listaSocios", listaSocios);
	}
	// TODO Mirar porque aparece el nombre de la vista en la URL

}